<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .layui-input-block {
            margin-left: 0px;
            width: 300px;
            display: inline-block;
        }
        .layui-form-label {
            width: 120px;
        }
        .gongneng{
            width: 100%;
            margin-left:180px;
        }
    </style>
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>

<body>
<div style="margin: 5% 5%">
    <div class="controller">
        <form class="layui-form" action="" onsubmit="return false" lay-filter="exampleedit">

            <div class="layui-form-item">
                <label class="layui-form-label">主题编号:</label>
                <div class="layui-input-block">
                    <input type="text" name="themeId" disabled="disabled" lay-verify="title" autocomplete="off" placeholder="请输入主题编号" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">主题名称:</label>
                <div class="layui-input-block">
                    <input type="text" name="themeName" lay-verify="title" autocomplete="off" placeholder="请输入主题名称" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">主题图片:</label>
                <div class="layui-input-block">
                    <img id="headImg" src="" width="100px" height="100px">
                </div>
            </div>
            <div class="layui-form-item" style="display: none">
                <label class="layui-form-label">主题图片:</label>
                <div class="layui-input-block">
                    <input type="text" name="themeImage" lay-verify="title" autocomplete="off" placeholder="请输入主题图片:" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">主题状态:</label>
                <div class="layui-input-block">
                    <select name="themeStatus" id="themeStatus">
                        <option value="启用">启用</option>
                        <option value="禁用">禁用</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <div class="gongneng">
                        <button type="submit" class="layui-btn" lay-submit="" lay-filter="updateedit">修改</button>
                        <a><button type="button" class="layui-btn layui-btn-normal" id="quxiao1">取消</button></a>
                    </div>
                </div>
            </div>

        </form>
    </div>
</div>
</body>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>
<script>

    /*获取地址栏的Id参数*/
    function GetRequest() {
        var url = decodeURI(location.search); //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for(var i = 0; i < strs.length; i ++) {
                theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

    var json=GetRequest();


        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , upload = layui.upload;



        $("input[name=themeId]").val(localStorage.getItem("themeId"));
        $("input[name=themeName]").val(localStorage.getItem("themeName"));
        $("input[name=themeImage]").val(localStorage.getItem("themeImage"));
        $("#headImg").attr("src", IMAGE_SERVER + "/" + localStorage.getItem("themeImage"));

        $("#themeStatus").val(localStorage.getItem("themeStatus"));
        layui.form.render('select');
        form.on('submit(updateedit)', function (data) {

            var theme = {
                themeId: $("input[name=themeId]").val(),
                themeName: $("input[name=themeName]").val(),
                themeImage: $("input[name=themeImage]").val(),
                themeStatus: $("select[name=themeStatus]").val()
            }
            $.ajax({
                url:"/admin/theme/update/theme",
                type:"put",
                dataType:"JSON",
                contentType:"application/json;charset=utf-8",
                data:JSON.stringify(theme),
                // beforeSend: function () {
                //     this.layerIndex = layer.load(0, {shade: [0.5, '#393D49']});
                // },
                success:function (res) {

                    if (res.code==200){
                        layer.msg("修改成功",{icom:5,time: 1000}
                            ,function () {

                                localStorage.removeItem("themeId" )
                                localStorage.removeItem("themeName" )
                                localStorage.removeItem("themeImage" )
                                localStorage.removeItem("themeStatus" )

                                location = '/admin/zhuti/zhuti.html';
                            }) ; //失败的表情
                        return ;
                    } else if(res.code==500){
                        layer.msg("修改失败"+res.data,{
                            icon:6,  //失败的表情
                            time:1000 //1秒关闭
                        });
                    }
                },
                error:function () {
                    layer.msg("网络原因修改失败",{
                        icon:2,time: 1000
                    })
                },
                complete:function () {
                    layer.close(this.layerIndex);
                }
            })
            // return false;

        });

        //文件上传
        // var upload = layui.upload;
        upload.render({
            elem: '#headImg',
            method: 'post',
            url: "/admin/fileinfo/upload/image",
            choose: function (obj) {
                fileName = null;
                //预览
                obj.preview(function (index, file, result) {
                    //$("#headImg").attr('src',result);
                    document.getElementById("headImg").src = result;
                });
            },
            done: function (res, index, upload) //上传完成
            {
                fileName = res.data; //传回来的新的文件名
                $("input[name=themeImage]").val(fileName);
            }
        });

    $("#quxiao1").click(function () {
        //location="#/layui/layer";
        // location.reload();
        location = '/admin/zhuti/zhuti.html';
    })
</script>
</html>